package com.sc.dao.impl;

import com.sc.dao.OrderItemDao;
import com.sc.entity.OrderItem;
import com.sc.utils.JDBCUtils;

import java.util.List;

public class OrderItemDaoImpl implements OrderItemDao {
    @Override
    public int add(OrderItem orderItem) throws Exception {
        String sql= "insert into order_item(order_id,product_id,product_num,price)values(?,?,?,?)";
        return JDBCUtils.update(sql,new Object[]{orderItem.getOrderId(),orderItem.getProductId(),
        orderItem.getProductNum(),orderItem.getPrice()});

    }

    @Override
    public int delete(int id) throws Exception {
        String sql = "delete from order_item where id = ?";
        return JDBCUtils.update(sql,new Object[]{id});
    }

    @Override
    public int update(OrderItem orderItem) throws Exception {
        String sql = "update order_item set order_id = ? ,product_id =?," +
                "product_num = ? ,price = ?";
        return JDBCUtils.update(sql,new Object[]{orderItem.getOrderId(),orderItem.getProductId(),
                orderItem.getProductNum(),orderItem.getPrice()});

    }

    @Override
    public OrderItem queryById(int id) throws Exception {
        String sql = "select from order_item where id = ?";
        return JDBCUtils.query(sql, OrderItem.class,new Object[id]);    }

    @Override
    public List<OrderItem> queryAll() throws Exception {
        String sql = "select from order_item";
        return JDBCUtils.querylist(sql,OrderItem.class,new Object[0]);
    }
}
